package cn.source.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import cn.source.system.domain.AaBanner;
import cn.source.system.domain.AaGoods;
import cn.source.system.mapper.AaCategoryDaoMapper;
import cn.source.system.mapper.AaGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.source.system.mapper.AaCategoryMapper;
import cn.source.system.domain.AaCategory;
import cn.source.system.service.IAaCategoryService;

/**
 * 分类管理Service业务层处理
 * 
 * @author sourcebyte.vip
 * @date 2023-08-04
 */
@Service
public class AaCategoryServiceImpl implements IAaCategoryService 
{
    @Autowired
    private AaCategoryMapper aaCategoryMapper;

    @Autowired
    private AaCategoryDaoMapper aaCategoryDaoMapper;

    @Autowired
    private AaGoodsMapper aaGoodsMapper;

    /**
     * 查询分类管理
     * 
     * @param id 分类管理主键
     * @return 分类管理
     */
    @Override
    public AaCategory selectAaCategoryById(Long id)
    {
        return aaCategoryMapper.selectAaCategoryById(id);
    }

    /**
     * 查询分类管理列表
     * @param aaCategory 分类管理
     * @return 分类管理
     */
    @Override
    public List<AaCategory> selectAaCategoryList(AaCategory aaCategory) {
        List<AaCategory> list = aaCategoryMapper.selectAaCategoryList(aaCategory);
        List<AaGoods> goods = aaGoodsMapper.selectAaGoodsList(null);
        for (AaCategory category : list) {
            List<AaGoods> cateGoods = goods.stream().filter(d ->
                    category.getId().equals(d.getCatetoryId())
                            || category.getId().equals(d.getCategoryTwoId())).collect(Collectors.toList());
            category.setGoodsNum(cateGoods.size());
            Long downNum = cateGoods.stream().filter(d -> Integer.valueOf(1).equals(d.getDownStatus())).count();
            category.setDownNum(downNum.intValue());
        }
        return list;
    }

    /**
     * 新增分类管理
     * @param aaCategory 分类管理
     * @return 结果
     */
    @Override
    public int insertAaCategory(AaCategory aaCategory)
    {
        return aaCategoryMapper.insertAaCategory(aaCategory);
    }

    /**
     * 修改分类管理
     * 
     * @param aaCategory 分类管理
     * @return 结果
     */
    @Override
    public int updateAaCategory(AaCategory aaCategory)
    {
        return aaCategoryMapper.updateAaCategory(aaCategory);
    }

    /**
     * 批量删除分类管理
     * 
     * @param ids 需要删除的分类管理主键
     * @return 结果
     */
    @Override
    public int deleteAaCategoryByIds(Long[] ids)
    {
        int count = 0;
        for (int i = 0; i < ids.length; i++) {
            AaCategory category = selectAaCategoryById(ids[i]);
            if (ObjectUtil.isNotEmpty(category)) {
                category.setDelFlag(1);
                count += aaCategoryMapper.updateAaCategory(category);
            }
        }
        return count;
//        return aaCategoryMapper.deleteAaCategoryByIds(ids);
    }

    /**
     * 删除分类管理信息
     * 
     * @param id 分类管理主键
     * @return 结果
     */
    @Override
    public int deleteAaCategoryById(Long id)
    {
        return aaCategoryMapper.deleteAaCategoryById(id);
    }

    @Override
    public List<AaCategory> selectAaCategoryByPid(Long pid) {
        return aaCategoryDaoMapper.selectAaCategoryByPid(pid);
    }
}
